package com.example.shixun1.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.example.shixun1.R;
import com.example.shixun1.activity.CartActivity;
import com.example.shixun1.activity.SettingsActivity;

public class MeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);

        // 获取“我的文章”“我的订单”“购物车”的布局
        View myArticlesLayout = view.findViewById(R.id.myArticlesLayout);
        View myOrdersLayout = view.findViewById(R.id.myOrdersLayout);
        View shoppingCartLayout = view.findViewById(R.id.shoppingCartLayout);

        // 为“我的文章”添加点击事件监听器
        myArticlesLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "点击了我的文章", Toast.LENGTH_SHORT).show();
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, new HeadlinesFragment());
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        // 为“我的订单”添加点击事件监听器
        myOrdersLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "点击了我的订单", Toast.LENGTH_SHORT).show();
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, new MallFragment());
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        // 为“购物车”添加点击事件监听器
        shoppingCartLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "点击了购物车", Toast.LENGTH_SHORT).show();
                // 跳转到购物车 Activity
                Intent intent = new Intent(getActivity(), CartActivity.class);
                startActivity(intent);
            }
        });
        View settingsLayout = view.findViewById(R.id.settingsLayout);
        settingsLayout.setOnClickListener(v -> {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
        });

        return view;
    }
}